import teamRepository from '@/repositories/team'

const getTeamListResult = async () => {
  let result = await teamRepository.getTeamList()
  return result
}

const getTeamsWithQuery = async (query) => {
  let result = await teamRepository.getTeamListWithQuery(query)
  return result
}

const addNewTeam = async (team) => {
  team.name = team.name.toUpperCase()
  return await teamRepository.addTeam(team)
}

const updateTeam = async (team) => {
  team.name = team.name.toUpperCase()
  return await teamRepository.updateTeam(team)
}

const deleteTeamById = async (teamId) => {
  return await teamRepository.deleteTeamById(teamId)
}

const getTeamEndorserListByTeamId = async (teamId) => {
  let team = await teamRepository.getTeamById(teamId)
  if (team !== null) {
    team.teamEndorsers.map((teamEndorser) => { teamEndorser.team = team.name })
    return team.teamEndorsers
  }
  return []
}

export default {
  getTeamListResult: getTeamListResult,
  getTeamsWithQuery: getTeamsWithQuery,
  addNewTeam: addNewTeam,
  updateTeam: updateTeam,
  deleteTeamById: deleteTeamById,
  getTeamEndorserListByTeamId: getTeamEndorserListByTeamId
}
